import { IFormProps, size,dateTimeFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Table, Button, Input, Select } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import moment from 'moment';

export interface IUserDividenProps extends IFormProps {
    user: any
}
class UserDividen extends React.PureComponent<IUserDividenProps, any>{
    constructor(props) {
        super(props);
        this.state = {}
    }
    
    componentDidMount() {
        this.getAllUserDividends();
    }

    search = () => {
        this.getAllUserDividends();
    }

    getAllUserDividends = (payload?) => {
        payload = Object.assign({}, this.props.user.allUserDividendsSearchs, this.props.form.getFieldsValue(), { ...payload });
        this.props.dispatch({
            type: "user/getAllUserDividends",
            payload
        });
    }
    view = (id) => {
        this.props.history.push('/user/userDividenDetail?id=' + id);
    }

    reset = () => {
        this.props.form.resetFields();
    }

    render(): JSX.Element {
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const {  allUserDividends: { totalCount, items },allUserDividendsSearchs: { pageIndex, pageSize,keywords,prizeType } } = this.props.user;
        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.user.allUserDividendsSearchs,
                {pageIndex, pageSize});

            this.getAllUserDividends(payload);
        }
        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };
        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                        <Form.Item label=' 用户名：'>
                                {getFieldDecorator("keywords", {
                                    initialValue: keywords
                                })(<Input
                                    style={{ width: 200 }}
                                    size={size}
                                    placeholder="请输入用户名"
                                />)}
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>用户分红记录列表(共 {totalCount} 条)</h3>

                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallUsersVOUserDividendsVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return (pageIndex - 1) * pageSize + index + 1
                                }} />
                                  <Table.Column title="头像" key="avatar" render={(text, record: api.SingleUserMallUsersVOUserDividendsVO, index) => {
                                    return record.avatar && <img width='80px' src={record.avatar} />
                                }} />
                                <Table.Column title="用户名称" key="name" dataIndex="name" />
                                <Table.Column title="总消费额" key="sumConsumptionAmount" dataIndex="sumConsumptionAmount" />
                                <Table.Column title="总分红利润" key="sumDividendAmount" dataIndex="sumDividendAmount"/>
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallUsersVOUserDividendsVO, index) => {
                                    return (
                                        <div>
                                            <a className={Styles.M_RIGHT} onClick={() => this.view(record.id)}>
                                                查看
                                            </a>
                                        </div>
                                    );
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
            </Row>)
    }
}
export default connect(({ user, loading: { models: { user: loading } } }) => ({ user, loading }))(Form.create()(UserDividen));
